package cn.kgc.vue.controller;

import cn.hutool.core.util.RandomUtil;
import cn.kgc.vue.annotation.LogAnnotation;
import cn.kgc.vue.commons.Const;
import cn.kgc.vue.commons.QueryCondition;
import cn.kgc.vue.commons.ResponseResult;
import cn.kgc.vue.entity.User;
import cn.kgc.vue.form.UserForm;
import cn.kgc.vue.service.UserService;
import cn.kgc.vue.enums.OperationType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 *    后端如何处理跨域问题
 *    1.  在允许跨域的方法上或类上添加注解 @CrossOrigin
 *         添加在方法上 只对当前方法有效
 *         添加在类上 则类中所有的方法均允许跨域
 */
@RestController
@Api(tags = "用户模块")
@RequestMapping("user")
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping("list")
    public List<User> selectUsers() {
        List<User> list = userService.list();
        return list;
    }


    // @CrossOrigin  //  该接口允许跨域请求
    @ApiOperation("验证码接口")
    @GetMapping("captcha")
    public ResponseResult getCaptcha() {
        return userService.getCaptcha();
    }


    //用户登录
    /*
    * 流程分析
    *     1、接受参数
    *     2、验证验证码
    *         1、uuid redis  code
    *            code  null   失效
    *            code  ==？ code
    *     3、shiro   subject login
    *
    * jwt json web token
    *
    *   Header       头部
    *   PayLoad      载荷
    *   Signature    签名
    *    xxxx·
    *
    * */
    @ApiOperation("用户认证")
    @PostMapping("login")
    public ResponseResult login(@RequestBody HashMap<String,Object> params, HttpServletRequest request) {
        return userService.login(params,request);
    }

    // 未认证 重定向的接口
    @GetMapping("unAuth")
    public ResponseResult unAuthentication() {
        ResponseResult responseResult = ResponseResult.fail().code(Const.UN_AUTH).message("请认证后访问");
        return responseResult;
    }

    @ApiOperation("用户分页")
    @PostMapping("page")
    public ResponseResult userPage(@RequestBody QueryCondition<User> queryCondition) {
        return userService.userPage(queryCondition);
    }

    @LogAnnotation(model="用户模块",operation= OperationType.UPDATE)
    @RequiresPermissions("sys:user:state")
    @PutMapping("state")
    @ApiOperation("改变状态")
    public ResponseResult state(@RequestBody HashMap<String,Object> params) {
        return userService.changeState(params);
    }


    @LogAnnotation(model="用户模块",operation= OperationType.DELETE)
    @RequiresPermissions("sys:user:delete")
    @ApiOperation("删除用户")
    @DeleteMapping("delete")
    public ResponseResult delete(@RequestBody List<Integer> ids){

        return userService.deleteUser(ids);
    }

    @LogAnnotation(model="用户模块",operation= OperationType.UPDATE)
    @ApiOperation("重置密码")
    @PutMapping("password/{id}")
    public ResponseResult password(@PathVariable("id") Integer id) {
        //生成随机盐
        String s = RandomUtil.randomString(8);
        String md5Password = new Md5Hash("111111", s, 10).toHex();
        User user = new User();
        user.setId(id);
        user.setPassword(md5Password);
        user.setSalt(s);

        userService.updateById(user);
        return ResponseResult.success().message("重置密码成功");
    }

    @LogAnnotation(model="用户模块",operation= OperationType.ADD)
    @RequiresPermissions("sys:user:add")
    @ApiOperation("添加用户")
    @PostMapping("add")
    public ResponseResult add(@RequestBody UserForm userForm) {

        return  userService.add(userForm);
    }

    @LogAnnotation(model="用户模块",operation= OperationType.UPDATE)
    @RequiresPermissions("sys:user:update")
   @ApiOperation("编辑用户")
    @PutMapping("edit")
    public ResponseResult edit(@RequestBody UserForm userForm) {
        return userService.edit(userForm);
   }

   @ApiOperation("获取个人信息")
    @GetMapping("personal")
    public ResponseResult personal(HttpServletRequest request) {
        return userService.personal(request);
   }

    @ApiOperation("编辑个人信息")
    @PutMapping("editPersonal")
    public ResponseResult editPersonal(@RequestBody User user) {
        userService.updateById(user);
        return ResponseResult.success().message("修改成功");
   }

    @ApiOperation("编辑个人密码")
    @PutMapping("editPersonalPassword")
    public ResponseResult editPersonalPassword(@RequestBody HashMap<String,String> params,HttpServletRequest request) {
        return  userService.editPersonal(params,request);
   }

   @ApiOperation("退出登录")
    @PutMapping("Logout")
    public ResponseResult Logout(HttpServletRequest request) {
        return  userService.Logout(request);
   }

   //获取用户权限菜单
    @ApiOperation("获取用户权限菜单")
    @GetMapping("menus")
    public ResponseResult get(HttpServletRequest request) {
        return userService.getMeuns(request);
    }











    }
